// packageA/pages/Visitormanagemen/Visitormanagemen.js
const app = getApp()
const {
  requestDataparams
} = require('../../../utils/request.js');
var util = require('../../../utils/util.js')
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: applet_park_api_url_imgUrl,
    visitorList: [],
    top: 0,
    height: 0,
    left: 0,
    showModal2: false,
    detail: {},
    date: '',
    start: '08:00',
    region: '',
    timeStart: '09:00',
    timeEnd: '18:00',
    userInfo: '',
    currentId: '',
    type: '',
    addressData: [],
    currentForeignType: '',
    currentForeignId: '',
    currentForeignName: '',
    currentIndex: 0,
    now: util.formatTime(new Date())
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initPage()
    this.getAdderss()
  },
  // 获取初始数据
  initPage() {
    requestDataparams('/api/visitorLetters', {
      type: 2
    }, 'GET').then(response => {
      // 
      if (response.data.code == 0) {
        var result = response.data.data
        console.log(result)
        for (var i in result) {
          if (result[i].visitStartTime) {
            result[i].visitStartTime = result[i].visitStartTime.substring(0, 16)
          }
          if (result[i].visitEndTime) {
            result[i].visitEndTime = result[i].visitEndTime.substring(11, 16)
          }
        }
        this.setData({
          visitorList: result
        })
      }
    })
  },
  // 地址selector切换
  addressChange(e) {

    // currentForeignType: '',
    // currentForeignId: '',
    // currentForeignName: ''
    // 
    this.setData({
      currentForeignType: this.data.addressData[e.detail.value].foreignType,
      currentForeignId: this.data.addressData[e.detail.value].id,
      currentForeignName: this.data.addressData[e.detail.value].name,
    })
  },
  // 延时
  addTime(e) {
    let id = e.currentTarget.dataset.id
    let time = e.currentTarget.dataset.item.visitStartTime
    if (util.formatTime(new Date()) >= util.formatTime(new Date()).substring(0, 10) + ' 22:00:00') {
      wx.showToast({
        title: '22:00后不可延时',
        icon: 'none'
      })
      return
    }
    if (time.substring(0, 10) != util.formatTime(new Date()).substring(0, 10)) {
      wx.showToast({
        title: '该访单已结束',
        icon: 'none'
      })
      return
    }
    let url = '/api/visitorLetters/' + id + '/delay'
    requestDataparams(url, {}, 'PUT').then(response => {
      if (response.data.code == 0) {
        wx.showToast({
          title: '延时至：' + response.data.data,
          icon: 'none',
          duration: 2000
        })
        this.initPage()
      }
    })
  },
  // 获取地址
  getAdderss() {
    // 
    requestDataparams('/api/gates/address', {}, 'GET').then(response => {

      if (response.data.code == 0 && response.data.data.length) {
        this.setData({
          addressData: response.data.data
        })
        this.setData({
          currentForeignType: this.data.addressData[0].foreignType,
          currentForeignId: this.data.addressData[0].id,
          currentForeignName: this.data.addressData[0].name,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 同意访客
  agreeHandel() {
    let visitStartTime = this.data.date + ' ' + this.data.timeStart + ':00'
    let visitEndTime = this.data.date + ' ' + this.data.timeEnd + ':00'
    let _this = this

    if (visitStartTime < this.data.now) {
      wx.showToast({
        title: '该前日期不可用',
        icon: 'none'
      })
      return
    }
    if (!this.data.date.length) {
      wx.showToast({
        title: '请选择日期',
        icon: 'none'
      })
      return
    }
    if (!this.data.timeStart.length) {
      wx.showToast({
        title: '请选择开始时间',
        icon: 'none'
      })
      return
    }
    if (!this.data.currentForeignName.length) {
      wx.showToast({
        title: '请选择地址',
        icon: 'none'
      })
      return
    }

    requestDataparams('/api/visitorLetters/' + this.data.currentId, {
      status: 2,
      visitStartTime,
      visitEndTime,
      type: this.data.type,
      foreignType: this.data.currentForeignType,
      foreignId: this.data.currentForeignId
    }, 'PUT', 'sign').then(response => {
      if (response.data.code == 0) {
        setTimeout(() => {
          _this.initPage()
        }, 50)
        _this.setData({
          showModal2: false,
          timeStart: '',
          timeEnd: '',
          currentForeignName: '',
          date: ''
        })
        wx.showToast({
          title: '同意访问',
          icon: 'success'
        })
      }
    })
  },
  clickHandle(e) {
    var that = this
    let type = e.currentTarget.dataset.type
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '是否确认拒绝',
      success(res) {
        if (res.confirm) {
          requestDataparams('/api/visitorLetters/' + id, {
            status: 3,
            reason: "拒绝",
            type: type
          }, 'PUT', 'sign').then(response => {
            if (response.data.code == 0) {
              wx.showToast({
                title: '拒绝成功',
                icon: 'success'
              })
              that.initPage()
              that.getAdderss()
            }
          })
        }
      }
    })
  },
  // 邀请函日期
  dateChange(e) {
    if (e.detail.value < util.formatTime(new Date()).substring(0, 10)) {
      wx.showToast({
        title: '该前日期不可用',
        icon: 'none'
      })
      return
    }
    this.setData({
      date: e.detail.value
    })
  },
  // 邀请开始时间
  timeStartChange(e) {
    this.setData({
      timeStart: e.detail.value
    })
  },
  // 邀请结束时间
  timeEndChange(e) {
    this.setData({
      timeEnd: e.detail.value
    })
    let startTime = this.data.timeStart.split(':')[0]
    let timeEnd = this.data.timeEnd.split(':')[0]
    if (startTime > timeEnd) {
      // let start  = this.data.startTime
      wx.showToast({
        title: '开始时间不能大于结束时间',
        icon: 'none'
      })
      this.setData({
        timeStart: '',
        timeEnd: ''
      })
      // let end = this.data.timeEnd
      // this.setData({
      //   timeStart: this.data.timeEnd
      // })
      //   this.setData({
      //     timeEnd: start

      //   }) 
    }
  },
  meetingdetail(e) {
    let id = e.currentTarget.dataset.id
    let type = e.currentTarget.dataset.type
    let d = new Date()
    let year = d.getFullYear()
    let month = d.getMonth() + 1
    let day = d.getDate()
    this.setData({
      showModal2: true,
      currentId: id,
      type: type,
      date: year + '-' + month + '-' + day,
      timeStart: '09:00',
      timeEnd: '18:00',
      // currentForeignName: '金城闸机'
    });
    // foreignType: this.data.currentForeignType,
    // foreignId: this.data.currentForeignId
  },
  // 关闭弹出层
  closedetail() {
    this.setData({
      showModal2: false
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initPage()
    this.getAdderss()
  },
  refuse() {
    wx.navigateTo({
      url: '../Visitorrefusal/Visitorrefusal',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})